// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Death Event Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 7 : TYPE_DEATHEVENT

private alias object.value1 : deathEvent.leftTextPos
private alias object.value2 : deathEvent.rightTextPos
private alias object.value3 : deathEvent.timer

event ObjectMain

	switch object.state
	case 0
	case 1
		temp0 = screen.xcenter
		temp0 -= 72
		if deathEvent.leftTextPos < temp0
			deathEvent.leftTextPos += 16
			if deathEvent.leftTextPos > temp0
				deathEvent.leftTextPos = temp0
			end if
		end if
		temp0 += 80
		if deathEvent.rightTextPos > temp0
			deathEvent.rightTextPos -= 16
			if deathEvent.rightTextPos < temp0
				deathEvent.rightTextPos = temp0
			end if
		end if
		if deathEvent.timer < 288
			deathEvent.timer += 4
		else
			if object.state == 0
				recStageList = stage.activeList
				recStage = stage.listPos
				stage.activeList = PRESENTATION_STAGE
				if options.vsMode == false
					if player.continues == 0
						stage.listPos = 0
						engine.state = 8
						CallNativeFunction2(ShowPromoPopup, 0, "GameOverPromo")
					else
						stage.listPos = 4
					end if
				else
					stage.listPos = 3
					if vs.playerID == 0
						if object.entityPos == 0
							player.score = 0
							player.lives = 3
							vs.time1P = 0xEA60
							vs.rings1P = 0
							vs.totalRings1P = 0
							vs.items1P = 0
						else
							player.score2 = 0
							player.lives2 = 3
							vs.time2P = 0xEA60
							vs.rings2P = 0
							vs.totalRings2P = 0
							vs.items2P = 0
						end if
					else
						if object.entityPos == 1
							player.score2 = 0
							player.lives2 = 3
							vs.time1P = 0xEA60
							vs.rings1P = 0
							vs.totalRings1P = 0
							vs.items1P = 0
						else
							player.score = 0
							player.lives = 3
							vs.time2P = 0xEA60
							vs.rings2P = 0
							vs.totalRings2P = 0
							vs.items2P = 0
						end if
					end if
				end if
				starPostID = 0
				if options.gameMode == 1
					arrayPos1 = options.saveSlot
					arrayPos1 <<= 3
					arrayPos1++
					if saveRAM[arrayPos1] < 3
						saveRAM[arrayPos1] = 3
					end if
					arrayPos1++
					saveRAM[arrayPos1] = 0
					arrayPos1++
					saveRAM[arrayPos1] = 0xC350
					player.score = 0
					WriteSaveRAM()
				end if
			else
				if options.vsMode == true
					if vs.playerID == 0
						vs.rings1P = object[0].value0
						vs.rings2P = object[1].value0
					else
						vs.rings1P = object[1].value0
						vs.rings2P = object[0].value0
					end if
					starPostID = 0
					stage.activeList = PRESENTATION_STAGE
					stage.listPos = 3
				end if
				if options.gameMode == 1
					arrayPos1 = options.saveSlot
					arrayPos1 <<= 3
					arrayPos1++
					saveRAM[arrayPos1] = player.lives
					WriteSaveRAM()
				end if
			end if
			if object.state == 1
				recMilliSeconds = 0
				recSeconds = 0
				recMinutes = 0
			end if
			LoadStage()
		end if
		if deathEvent.timer > 0
			music.volume -= 2
			SetScreenFade(0, 0, 0, deathEvent.timer)
		else
			if inputPress.buttonA == true
				deathEvent.timer = 0
			end if
			if inputPress.buttonB == true
				deathEvent.timer = 0
			end if
			if inputPress.buttonC == true
				deathEvent.timer = 0
			end if
			CheckTouchRect(0, 0, screen.xsize, screen.ysize)
			if checkResult > -1
				deathEvent.timer = 0
			end if
		end if
		break
	case 2
		if deathEvent.timer < 288
			if options.bossGame == 1
				CheckCurrentStageFolder("Zone13")
				if checkresult == 1
					music.volume -= 2
				else
					music.volume -= 0
				endif
				vs.rings1P = 0
				vs.time1P = stage.seconds
				vs.time1P *= 100
				temp2 = stage.minutes
				temp2 *= 0x1770
				vs.time1P += temp2
				vs.time1P += stage.milliSeconds
				vs.items1P = 0
				vs.speed = object[0].speed
			else
				music.volume -= 2
			endif
			deathEvent.timer += 4
		else
			if options.gameMode == 1
				arrayPos1 = options.saveSlot
				arrayPos1 <<= 3
				arrayPos1++
				saveRAM[arrayPos1] = player.lives
				WriteSaveRAM()
			end if
			LoadStage()
		end if
		SetScreenFade(0, 0, 0, deathEvent.timer)
		break
	case 3
		if deathEvent.timer < 288
			music.volume -= 2
			deathEvent.timer += 4
		else
			timeAttack.result = 0
			vs.bossAttack = 0
			player.score = 0
			starPostID = 0
			recMilliSeconds = 0
			recSeconds = 0
			recMinutes = 0
			stage.milliSeconds = 0
			stage.seconds = 0
			stage.minutes = 0
			engine.state = 8
		end if
		SetScreenFade(0, 0, 0, deathEvent.timer)
		break
	end switch
end event


event ObjectDraw
	switch object.state
	case 0
		DrawSpriteScreenXY(0, deathEvent.leftTextPos, 96)
		DrawSpriteScreenXY(2, deathEvent.rightTextPos, 96)
		break
	case 1
		DrawSpriteScreenXY(1, deathEvent.leftTextPos, 96)
		DrawSpriteScreenXY(2, deathEvent.rightTextPos, 96)
		break
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(0, 0, 64, 16, 1, 239)
	SpriteFrame(0, 0, 56, 16, 66, 239)
	SpriteFrame(0, 0, 64, 16, 123, 239)
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
